package com.yjlove.myplay;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.message.common.UmLog;
import com.umeng.message.inapp.IUmengInAppMsgCloseCallback;
import com.umeng.message.inapp.InAppMessageManager;
import com.yjlove.myplay.ui.activity.AMapTestActivity;
import com.yjlove.myplay.ui.activity.BannerTestActivity;
import com.yjlove.myplay.ui.activity.EmailTestActivity;
import com.yjlove.myplay.ui.activity.GetPictureActivity;
import com.yjlove.myplay.ui.activity.GlideTestActivity;
import com.yjlove.myplay.ui.activity.ImageLoaderTestActivity;
import com.yjlove.myplay.ui.activity.MPAndroidTestActivity;
import com.yjlove.myplay.ui.activity.PullToRefreshTestActivity;
import com.yjlove.myplay.ui.activity.RecyclerViewTestActivity;
import com.yjlove.myplay.ui.activity.TimerTestActivity;
import com.yjlove.myplay.ui.activity.UITestActivity;
import com.yjlove.myplay.ui.activity.VPIndicatorTestActivity;
import com.yjlove.myplay.ui.activity.WebViewTestActivity;
import com.yjlove.myplay.ui.activity.WebViewX5TestActivity;
import com.yjlove.myplay.ui.activity.ZXingTestActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.btn_rv)
    Button mRv;
    @Bind(R.id.btn_wv)
    Button mWv;
    @Bind(R.id.btn_wvx5)
    Button mWvX5;
    @Bind(R.id.btn_il)
    Button mLi;
    @Bind(R.id.btn_ptr)
    Button mPtr;
    @Bind(R.id.btn_amap)
    Button mBtnAmap;
    @Bind(R.id.btn_mpandroid)
    Button mBtnMpandroid;
    @Bind(R.id.btn_vpindicator)
    Button mBtnVpindicator;
    @Bind(R.id.btn_timer)
    Button mBtnTimer;
    @Bind(R.id.btn_glide)
    Button mBtnGlide;
    @Bind(R.id.btn_zxing)
    Button mBtnZxing;
    @Bind(R.id.btn_picture)
    Button mBtnPicture;
    @Bind(R.id.btn_banner)
    Button mBtnBanner;
    @Bind(R.id.btn_email)
    Button mBtnEmail;
    @Bind(R.id.btn_ui)
    Button mBtnUi;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = getApplicationContext();

        mRv.setOnClickListener(this);
        mWv.setOnClickListener(this);
        mWvX5.setOnClickListener(this);
        mLi.setOnClickListener(this);
        mBtnGlide.setOnClickListener(this);
        mPtr.setOnClickListener(this);
        mBtnAmap.setOnClickListener(this);
        mBtnMpandroid.setOnClickListener(this);
        mBtnVpindicator.setOnClickListener(this);
        mBtnTimer.setOnClickListener(this);
        mBtnZxing.setOnClickListener(this);
        mBtnPicture.setOnClickListener(this);
        mBtnBanner.setOnClickListener(this);
        mBtnEmail.setOnClickListener(this);
        mBtnUi.setOnClickListener(this);

        // 友盟统计判断当前页面为活动页
        PushAgent.getInstance(mContext).onAppStart();

        // 应用内消息
        InAppMessageManager.getInstance(this).showCardMessage(this, "main",
                new IUmengInAppMsgCloseCallback() {
                    //插屏消息关闭时，会回调该方法
                    @Override
                    public void onColse() {
                        UmLog.i("MainActivity", "card message close");
                    }
                });

        MobclickAgent.setDebugMode(false);//开启/关闭调试模式（true，如果不开启debug运行不会上传umeng统计）
        MobclickAgent.setCheckDevice(false);//不采集手机mac地址
        MobclickAgent.openActivityDurationTrack(false);// 禁止默认的页面统计方式，这样将不会再自动统计Activity。
        MobclickAgent.setScenarioType(mContext, MobclickAgent.EScenarioType.E_UM_NORMAL);//设置场景模式为普通
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainActivity");
        MobclickAgent.onResume(mContext);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainActivity");
        MobclickAgent.onPause(mContext);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_rv:
                Intent rvIntent = new Intent(mContext, RecyclerViewTestActivity.class);
                rvIntent.putExtra("title", mRv.getText().toString());
                startActivity(rvIntent);
                break;
            case R.id.btn_wv:
                Intent wvIntent = new Intent(mContext, WebViewTestActivity.class);
                wvIntent.putExtra("title", mWv.getText().toString());
                startActivity(wvIntent);
                break;
            case R.id.btn_wvx5:
                Intent wvx5Intent = new Intent(mContext, WebViewX5TestActivity.class);
                wvx5Intent.putExtra("title", mWvX5.getText().toString());
                startActivity(wvx5Intent);
                break;
            case R.id.btn_il:
                Intent ilIntent = new Intent(mContext, ImageLoaderTestActivity.class);
                ilIntent.putExtra("title", mLi.getText().toString());
                startActivity(ilIntent);
                break;
            case R.id.btn_glide:
                Intent glideIntent = new Intent(mContext, GlideTestActivity.class);
                glideIntent.putExtra("title", mBtnGlide.getText().toString());
                startActivity(glideIntent);
                break;
            case R.id.btn_ptr:
                Intent ptrIntent = new Intent(mContext, PullToRefreshTestActivity.class);
                ptrIntent.putExtra("title", mPtr.getText().toString());
                startActivity(ptrIntent);
                break;
            case R.id.btn_amap:
                Intent amapIntent = new Intent(mContext, AMapTestActivity.class);
                amapIntent.putExtra("title", mBtnAmap.getText().toString());
                startActivity(amapIntent);
                break;
            case R.id.btn_mpandroid:
                Intent mpandroidIntent = new Intent(mContext, MPAndroidTestActivity.class);
                mpandroidIntent.putExtra("title", mBtnMpandroid.getText().toString());
                startActivity(mpandroidIntent);
                break;
            case R.id.btn_vpindicator:
                Intent vpindicatorIntent = new Intent(mContext, VPIndicatorTestActivity.class);
                vpindicatorIntent.putExtra("title", mBtnVpindicator.getText().toString());
                startActivity(vpindicatorIntent);
                break;
            case R.id.btn_timer:
                Intent timerIntent = new Intent(mContext, TimerTestActivity.class);
                timerIntent.putExtra("title", mBtnTimer.getText().toString());
                startActivity(timerIntent);
                break;
            case R.id.btn_zxing:
                Intent zxingIntent = new Intent(mContext, ZXingTestActivity.class);
                zxingIntent.putExtra("title", mBtnZxing.getText().toString());
                startActivity(zxingIntent);
                break;
            case R.id.btn_picture:
                Intent pictureIntent = new Intent(mContext, GetPictureActivity.class);
                pictureIntent.putExtra("title", mBtnPicture.getText().toString());
                startActivity(pictureIntent);
                break;
            case R.id.btn_banner:
                Intent bannerIntent = new Intent(mContext, BannerTestActivity.class);
                bannerIntent.putExtra("title", mBtnBanner.getText().toString());
                startActivity(bannerIntent);
                break;
            case R.id.btn_email:
                Intent emailIntent = new Intent(mContext, EmailTestActivity.class);
                emailIntent.putExtra("title", mBtnEmail.getText().toString());
                startActivity(emailIntent);
                break;
            case R.id.btn_ui:
                Intent uiIntent = new Intent(mContext, UITestActivity.class);
                uiIntent.putExtra("title", mBtnUi.getText().toString());
                startActivity(uiIntent);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            Hook();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // /对于好多应用，会在程序中杀死 进程，这样会导致我们统计不到此时Activity结束的信息，
    // /对于这种情况需要调用 'MobclickAgent.onKillProcess( Context )'
    // /方法，保存一些页面调用的数据。正常的应用是不需要调用此方法的。
    private void Hook() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("退出应用", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                MobclickAgent.onKillProcess(mContext);

                int pid = Process.myPid();
                Process.killProcess(pid);
            }
        });
        builder.setNeutralButton("后退一下", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                finish();
            }
        });
        builder.setNegativeButton("点错了", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        builder.show();
    }
}
